import QtQuick 2.2
import QtQuick.Controls 1.1
import CIoWork 1.0
import QtQuick.Dialogs 1.2

ApplicationWindow {
    id: applicationWindow
    visible: true
    width: 640
    height: 480
    title: qsTr("Quick Serial")

    onClosing:  {
        console.log("exit!!!!")
        ioWorker.close()
        console.log("closed the serial port!")
    }

    CIoWork {
        id: ioWorker
        data: serialIn.text
    }

    FileDialog {
        id: fileDlg
        selectMultiple: false
        selectFolder: false
        selectExisting: false
        title: "Save the output content"

        nameFilters: [ "Text files (*.txt)", "All files (*)" ]

        onAccepted: {

        }
        onRejected: {

        }
    }

    menuBar: MenuBar {
        Menu {
            title: qsTr("File")

            MenuItem {
                text: qsTr("Save Output")
                onTriggered: {
                    fileDlg.open()
                }
            }

        }
    }

    Rectangle {
        id: outRect
        x: 5
        width: 630
        height: 215
        anchors.topMargin: 10
        anchors.top: inRect.bottom
        border.width: 1
        border.color: "#8080F0"

        Flickable {
            id: outFlick
            anchors.fill: parent
            width: outRect.width
            height: outRect.height
            enabled: true
            contentWidth: serialOut.width
            contentHeight: serialOut.height

            function ensureVisible() {
                if (height < serialOut.height) {
                    contentY= serialOut.height-height
                }
            }

            TextEdit {
                id: serialOut
                x: 2
                y: 1
                width: outFlick.width-4
                readOnly: true
                //focus: true
                color: "#c72400"
                text: text + ioWorker.data

                horizontalAlignment: Text.AlignLeft
                verticalAlignment: Text.AlignTop
                wrapMode: Text.WordWrap
                transformOrigin: Item.TopLeft
                onTextChanged: outFlick.ensureVisible()
            }
        }


    }

    Rectangle {
        id: inRect
        x: 5
        width: 630
        height: 215
        border.width: 1
        border.color: "#8080F0"

        Flickable {
            id: inFlick
            anchors.fill: parent
            width: inRect.width
            height: inRect.height
            contentWidth: serialIn.width
            contentHeight: serialIn.height
            //flickableDirection: Flickable.verticalFlick

            function ensureVisible(rect) {
                 if (contentX >= rect.x)
                     contentX = rect.x;
                 else if (contentX+width <= rect.x+rect.width)
                     contentX = rect.x+rect.width-width;
                 if (contentY >= rect.y)
                     contentY = rect.y;
                 else if (contentY+height <= rect.y+rect.height)
                     contentY = rect.y+rect.height-height;
            }

            TextEdit {
                id: serialIn
                x: 2
                y: 1
                width: inFlick.width-4
                color: "#0400c7"
                text: ""
                transformOrigin: Item.TopLeft
                horizontalAlignment: Text.AlignLeft
                verticalAlignment: Text.AlignTop
                wrapMode: Text.WordWrap
                font.pixelSize: 12
                focus: true

                onCursorRectangleChanged: inFlick.ensureVisible(cursorRectangle)
            }
        }
    }
}
